package com.bot.core;
import java.sql.*;

public class SqlUsers {
	Connection conn;
	
	public SqlUsers() throws Exception {
		Class.forName("org.sqlite.JDBC");
	   try {
		this.conn = DriverManager.getConnection("jdbc:sqlite:lost.db");
	   } catch (Exception e) {
		   System.out.println("Error: " + e.getMessage());
	   }
	}
	
	public synchronized int getAmountOfPushesPerNick(String nickname) throws Exception  {
	    Statement stat = conn.createStatement();
	    ResultSet resultSet = stat.executeQuery("SELECT COUNT(1) from user_pushes WHERE nickname=\"" + nickname + "\"");
	
	    resultSet.next();
	    int sum = resultSet.getInt(1); 
	    
	    resultSet.close();
	    
	    return sum;
	}
	
	public void insertPushToUser(String nickname, String date, int seconds) throws SQLException {
		Statement stat = conn.createStatement();
	    stat.executeUpdate("INSERT INTO user_pushes VALUES (\"" + seconds + "\", \"" + nickname + "\", \"" + date + "\")");
	}
	
//	public void isValidAdmin(String user, String passwd) {
//		Statement stat = conn.createStatement();
//	    ResultSet resultSet = stat.executeQuery("SELECT passwd from admins WHERE username=\"" + user + "\"");
//	
//	    resultSet.next();
//	    int sum = resultSet.getInt(1); 
//	    
//	    resultSet.close();
//	    
//	    return sum;
//	}

	
	public void finalize() {
		try {
			this.conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}